import {Color, DoubleSide, Group, Mesh, MeshBasicMaterial, PlaneGeometry} from "three";
import {releaseMesh} from "../../../framework/util.ts";


export class PointGroup extends Group{
    width: number
    height: number

    static backgroundGap: number = 0.005 // 覆盖物与背景之间的间隙
    static backgroundEnable = false
    static backgroundColor: number = 0x78b1e5
    static useRandomBackgroundColor = true

    background?: Mesh

    constructor(width: number, height: number) {
        super();
        this.width = width
        this.height = height
        if (PointGroup.backgroundEnable) {
            this.genBasePlane()
        }
    }

    genBasePlane() {
        let color: Color
        if (PointGroup.useRandomBackgroundColor) {
            color = new Color(Math.random(), Math.random(), Math.random())
        } else {
            color = new Color(PointGroup.backgroundColor)
        }
        let height = this.height
        let width = this.width
        let baseGeo = new PlaneGeometry(width, height, width-1, height-1)
        let baseMat = new MeshBasicMaterial({
            color: color,
            side: DoubleSide
        })
        let baseMesh = new Mesh(baseGeo, baseMat)
        baseMesh.position.set(0, 0, 0.002)
        this.background = baseMesh
        this.add(baseMesh)
    }

    dispose() {
        if (this.background) {
            releaseMesh(this.background)
        }
    }
}